package com.coderg.gmall.list.controller;

import com.coderg.gmall.common.result.Result;
import com.coderg.gmall.list.service.GoodsService;
import com.coderg.gmall.model.list.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/list")
public class ListApiController {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;
    @Autowired
    GoodsService goodsService;

    /**
     * @return
     */
    @GetMapping("/createIndex")
    public Result createIndex() {
        restTemplate.createIndex(Goods.class);
        restTemplate.putMapping(Goods.class);
        return Result.ok();
    }

    /**
     * 将数据库数据导入es  上架
     *
     * @param goodsId
     */
    @GetMapping(value = "/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable("skuId") Long goodsId) {
        goodsService.addSkuFromDbToEs(goodsId);
        return Result.ok();
    }

    /**
     * 下架商品
     * @param skuId
     * @return
     */
    @GetMapping("/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable("skuId") Long skuId) {
        goodsService.lowerGoods(skuId);
        return Result.ok();
    }

    /**
     * 更新商品热点incrHotScore
     *
     * @param skuId
     * @return
     */
    @GetMapping("/incrHotScore/{skuId}")
    public Result incrHotScore(@PathVariable("skuId") Long skuId){
        goodsService.incrHotScore(skuId);
        return Result.ok();
    }
    /**
     * 查询商品
     *
     * @param searchDate
     * @return
     */
    @PostMapping("/getGoods")
    public Result getGoodsByCondition(@RequestBody Map<String,Object> searchDate){
        Map<String, Object> map = goodsService.searchGoods(searchDate);
        return Result.ok(map);
    }

}
